<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$basic = $this->plugin_setting('basic');
if($_RQ['op']=='main'){
	set_html('title',$basic['title']?'视频列表-'.$basic['title']:'视频列表');
	$return['sharedata']=array(
		'title'=>$basic['share_title'],
		'desc'=>$basic['share_desc'],
		'link'=>custom_url('jy_kexie/video/',true),
		'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
	$return['params']=array('groupid'=>0,'keyword'=>'');
	if($_RQ['keyword']){
		$return['params']['keyword']=$_RQ['keyword'];
	}
	$return['groups']=pdo_getall('jy_kexie_video_group',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'));
	foreach($return['groups'] as &$gv){
		$gv['cids']=pdo_getall('jy_kexie_video_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$gv['id']),array('id','title'));
	}
	$return['cids']=array();
	exi($return);
}
if($_RQ['op']=='getlist'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0);	
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);	
	}
	if($_RQ['groupid']){
		$params['group_id']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];		
	}
	if($_RQ['cid']){
		$params['cid']=intval($_RQ['cid']);
		$return['params']['cid']=$_RQ['cid'];		
	}
	if($_RQ['isnews']){
		$params['video']='';
		$return['params']['isnews']=$_RQ['isnews'];
	}
	if($_RQ['isvideo']){
		$params['video <>']='';
		$return['params']['isvideo']=$_RQ['isvideo'];
	}
	if($_RQ['keyword']){
		$params['title like']='%'.$_RQ['keyword'].'%';
		$return['params']['keyword']=$_RQ['keyword'];		
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_kexie_video',$params,array($pindex,$psize),$total,$fields,'','updatetime desc');
	foreach($list as &$kv){
			$kv['thumb']=tomedia($kv['thumb']);
	}	
	//按点赞数排序作品
	if($list){
		$sum=count($list);
		foreach($list as &$item){
			$item['updatetime']=$item['updatetime']?:$item['createtime'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$member=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','avatar'));
			$item['avatar']=$item['avatar']?$item['avatar']:$member['avatar'];
			$item['thumb']=tomedia($item['thumb']);
			$lasttime=TIMESTAMP-$item['updatetime'];
			if($lasttime>24*3600){
				$item['updatetimetitle']=intval($lasttime/24*3600).'天前';
			}elseif($lasttime>=3600){
				$item['updatetimetitle']=intval($lasttime/3600).'小时前';
			}else{
				$item['updatetimetitle']=intval($lasttime/60).'分钟前';
			}
			$video_log=pdo_get('jy_kexie_video_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$item['id']));
			$item['video_log']=$video_log;
			$item['likes']=pdo_count('jy_kexie_video_log',array('video_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'islike'=>1),'id');
		}
	}
	$return['list']=$list;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='looklog'){
	$video_id=intval($_RQ['id']);
	$video_log=pdo_get('jy_kexie_video_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$video_id));
	if(!$video_log['id']){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'video_id'=>$video_id,
			'isread'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_kexie_video_log',$insert);		
	}
	exi('success');
}